﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VictorBlaga.DocumentManagement.Infrastructure.DataAccess;

namespace VictorBlaga.DocumentManagement
{
    public class NHibernateModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += BeginTransaction;
            context.EndRequest += CloseTransactionAndSession;
        }

        private void BeginTransaction(object sender, EventArgs e)
        {
            NHibernateHelper.Instance.BeginTransaction();
        }

        private void CloseTransactionAndSession(object sender, EventArgs e)
        {
            NHibernateHelper.Instance.CommitTransaction();
            NHibernateHelper.Instance.CloseSession();
        }

        public void Dispose() { }
    }
}